<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use think\captcha\Captcha;
use think\Controller;
use think\Request;

class Login extends Controller
{

    public function back(){
        if(\request()->isPost()){
            $data = input('post.');

            //验证码验证码
            $captcha = new Captcha();
            if (!$captcha->check($data['captcha'])){
                return $this->error("验证码错误");
            }

            $admin = Admin::get(['name' => $data['name']]);
            if (!$admin){
                $this->error('没有该管理员');exit;
            }
            $pwd = Admin::get(['password' => md5($data['password'])]);
            if (!$pwd){
                $this->error('原密码错误');exit;
            }
            $admin->password = md5($data['new_password']);
            if (!$admin->save()){
                $this->error('修改失败');exit;
            }
            $this->success('修改成功',url('login/index'));exit;
        }
        return view();
    }

   public function index(){
       $admin = new Admin();
       if(request()->isPost()){
        //    $this->check(input('code'));
           $data = input('post.');
           $res = $admin->login($data);

           //验证码验证码
           $captcha = new Captcha();
           if (!$captcha->check($data['captcha'])){
               return $this->error("验证码错误");
           }

           if($res['valid']){
            $this->success($res['msg'],url('index/index'));exit;
           }else{
            $this->error($res['msg']);exit;
           }
           return;
       }
       return view();
   }
}
